package com.pro.service.imple;

import com.pro.mapper.CensusMapper;
import com.pro.pojo.Census;
import com.pro.pojo.ProductCategroy;
import com.pro.service.CensusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * auther 杨沛琪
 * createDate 2022/1/21
 */
@Service
public class CensusServiceImple implements CensusService {
    @Autowired
    private CensusMapper censusMapper;

    @Override
    public List<Census> getCensus(Census census) throws Exception {
        if (census.getField() == null || "".equals(census.getField())){
            throw new Exception("字段不能为空");
        }
        return censusMapper.getCensus(census);
    }
    //查询商品名称
    @Override
    public List<ProductCategroy> getTitle() {
        return censusMapper.getTitle();
    }
    //查询商品点击数
    @Override
    public List<ProductCategroy> getHits() {
        return censusMapper.getHits();
    }
    //查询商品收藏数
    @Override
    public List<ProductCategroy> getCollection() {
        return censusMapper.getCollection();
    }

    //统计关键字
    @Override
    public List<Map<String, Object>> getGetKeyword() {
        List<Map<String, Object>> getKeyword = censusMapper.getGetKeyword("js_search_keywords","search_keywords","countNum");
        return getKeyword;
    }
    //获取订单数
    @Override
    public List<Census> getOrderNum() {
        return censusMapper.getOrderNum();
    }
    //获取订单数
    @Override
    public List<Census> getCourseOrderNum() {
        return censusMapper.getCourseOrderNum();
    }
    //统计商品评论数
    @Override
    public List<Census> getCommodityCommentNum() {
        return censusMapper.getCommodityCommentNum();
    }
    //统计课程评论数
    @Override
    public List<Census> getCourseCommentNum() {
        return censusMapper.getCourseCommentNum();
    }
    //统计文章评论数
    @Override
    public List<Census> getArticleCommentNum() {
        return censusMapper.getArticleCommentNum();
    }

    @Override
    public Integer getFrontEndNumber(String tableName) {
        Integer frontEndNumber = censusMapper.getFrontEndNumber(tableName);
        return frontEndNumber;
    }

    @Override
    public List<Object> getMonthlyNumber(String tabName, String dateName) {
        List<Map<String,Object>> monthlyNumber = censusMapper.getMonthlyNumber(tabName,dateName);
        Object[] obj = new Object[12];
        for (Map<String, Object> stringIntegerMap : monthlyNumber) {
            int currentMonth = Integer.parseInt(stringIntegerMap.get("currentMonth").toString());
            obj[currentMonth-1] = stringIntegerMap.get("num").toString();
        }
        for(int i = 0; i<obj.length; i++){
            if(obj[i] == null){
                obj[i] = 0;
            }
        }
        return Arrays.asList(obj);
    }
}
